package servlets;



import bean.Goods;
import com.alibaba.fastjson.JSON;
import dao.GoodsDao;
import utils.Response;
import utils.ResponseCode;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class GetAllGoodsesServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        该方法的作用是向用户返回所有的商品数据
        resp.setContentType("application/json;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
//        第一步：获取所有商品
        List<Goods> goodsList=null;
        try{
            goodsList= GoodsDao.findAll();
            Response<List<Goods>> build = Response.build(ResponseCode.SUCCESS, goodsList);
            resp.getWriter().write(JSON.toJSON(build).toString());
        }catch (SQLException e){
            e.printStackTrace();
            Response<List<Goods>> build = Response.build(ResponseCode.ERROR, goodsList);
            resp.getWriter().write(JSON.toJSON(build).toString());
        }



    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }


}
